
<%@include file="/html/portlet/init.jsp" %>
<portlet:actionURL name="saveAccountDetails" var="saveAccountDetails" />
<%	
	Calendar cal=Calendar.getInstance();
	int year=cal.get(Calendar.YEAR);
	String ccList = "ccList";
			
	List<CreditCard> creditCards=CreditCardLocalServiceUtil.getByUserId_CompanyId(themeDisplay.getUserId(), themeDisplay.getCompanyId());
	
	if (Validator.isNull(creditCards) || creditCards.isEmpty() ) {
		List<CreditCard> newList = new ArrayList<CreditCard>();
		newList.add(new CreditCardImpl());
		creditCards = newList;	
	}
%>
<div >
	<liferay-ui:error key="cvvKey" message="Invalid CVV" />
	<liferay-ui:error key="expDate" message="Date is not valid" />
	<liferay-ui:error key="lastKey" message="Last name is empty" />
	<liferay-ui:error key="firstKey" message="First name is empty" />
	<liferay-ui:error key="cardNumKey" message="Invalid Card Number" />
	<liferay-ui:error key="emptyDate" message="Date is not selected" />
</div>
<div class="portlet-msg-info">
	<liferay-ui:message	key="All Fields are mendatory" />
</div>
<aui:form name="fm" action="<%= saveAccountDetails.toString() %>"  method="post" >
	<aui:fieldset  cssClass="<%= renderResponse.getNamespace() + ccList %>">
<%
	int count=1;
	for(CreditCard creditCard:creditCards){
		String decryptedCardNumber = "",decryptedCardCVV="";
		Company comp = CompanyLocalServiceUtil.getCompanyById(themeDisplay.getCompanyId());
		decryptedCardNumber = Encryptor.decrypt(comp.getKeyObj(), creditCard.getCardNumber());
		decryptedCardCVV	= Encryptor.decrypt(comp.getKeyObj(), creditCard.getCardCVV());
%>
	<div class="lfr-form-row lfr-form-row-inline" >
		<aui:fieldset cssClass="aui-w33">
			<aui:input type="hidden" name='<%= "cardId" + count %>' value="<%= creditCard.getCardId() %>"/>
				<aui:input size="15"  fieldParam="cardFirstName"  name='<%= "cardFirstName" + count %>' value="<%= creditCard.getCardFirstName() %>" label="First Name" onkeypress="return checkAlpha(event,/[a-zA-Z ]/);">
					<aui:validator name="required"	errorMessage="First name cannot be empty" />
				</aui:input>
				<aui:input size="15"  fieldParam="cardLastName" name='<%= "cardLastName" + count %>'	value="<%= creditCard.getCardLastName() %>" label="Last Name" onkeypress="return checkAlpha(event,/[a-zA-Z ]/);">
					<aui:validator name="required"	errorMessage="Last name cannot be empty" />
				</aui:input>
		</aui:fieldset>	
		<aui:fieldset cssClass="aui-w33">
				<aui:input size="16" maxLength="16" fieldParam="cardNumber" name='<%= "cardNumber" + count %>' value="<%= decryptedCardNumber %>" label="Card Number" onkeypress="return checkZero(event,this,/[0-9]/);">
				   <aui:validator name="required" errorMessage="enter card number" />
				   <aui:validator name="rangeLength">[13,17]</aui:validator>
				   <aui:validator name="digits" />
				</aui:input>
		</aui:fieldset>
<%
	int expMonth =CardValidation.convertToInt(creditCard.getCardExpMonth());
	int expYear = CardValidation.convertToInt(creditCard.getCardExpYear());
	pageContext.setAttribute("mm", expMonth);
	pageContext.setAttribute("yyyy", expYear); 
%>
		<aui:fieldset cssClass="aui-w33">
			<aui:select name='<%= "expMM" + count %>' label="Exp. Month">
					<c:forEach begin="1" end="12" var="count">
						<c:choose>
						 	<c:when test="${count == mm}">
								<c:choose>
									<c:when test="${count < 10 }">
										<aui:option selected="true" value="0${count}">0${count}</aui:option>													
									</c:when>
									<c:otherwise>
										<aui:option selected="true" value="${count}">${count}</aui:option>
									</c:otherwise>
								</c:choose>
							</c:when>
							<c:otherwise>
								<c:choose>
									<c:when test="${count < 10 }">
										<aui:option value="0${count}">0${count}</aui:option>													
									</c:when>
									<c:otherwise>
										<aui:option value="${count}">${count}</aui:option>
									</c:otherwise>
								</c:choose>
							</c:otherwise>
						</c:choose>
					</c:forEach>
				</aui:select>		
				<aui:select name='<%= "expYY" + count %>' label="Exp. Year" >
					<c:forEach begin="<%= year %>" end = "2050" var = "count" >
						<c:choose>
							<c:when test="${count == yyyy}">
								<aui:option selected="true" value="${count}">${count}</aui:option>
							</c:when>
							<c:otherwise>
								<aui:option value="${count}">${count}</aui:option>
							</c:otherwise>
						</c:choose>						
					</c:forEach>
				</aui:select>
		</aui:fieldset>
		<aui:fieldset cssClass="aui-w33">
				<aui:input size="3" maxLength="3" fieldParam="CVV"  name='<%= "CVV" + count %>' label="CVV" value="<%= decryptedCardCVV %>" onkeypress="return checkZero(event,this,/[0-9]/);">
					<aui:validator name="required"	errorMessage="cvv number can not be empty" />
					<aui:validator name="rangeLength"	errorMessage="should be only 3 character ">[3,3]</aui:validator>
					<aui:validator name="digits" errorMessage="Only digits are allowed" />
				</aui:input>
				<aui:input  checked="<%= creditCard.getDefaultEntry() %>" label="primary" name= "defaultEntry" type="radio"
				 value="<%= count %>" helpMessage="Select for making this account as primary for payment" >
				 	<aui:validator name="required" errorMessage="Select at least one Primary Account " />
				</aui:input>
		</aui:fieldset>
	</div>
	<%	count++; 
	}
	%>	
		<aui:button-row>
			<aui:button type="Submit" value="Save" name="Submit"  />
		</aui:button-row>
	</aui:fieldset>
</aui:form>

<aui:script use="hoperay-auto-fields">
	 new Liferay.HopeAutoFields({
                    contentBox: 'fieldset.<portlet:namespace />ccList',
                    fieldIndexes: '<portlet:namespace />ccListIndexes',  
        }
	).render();
</aui:script>
